package com.junxonline.instant.api.config;

import com.junxonline.instant.api.listener.LogApiListener;
import com.junxonline.instant.common.constant.RedisMqChannelConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * Redis消息配置类
 *
 * @author JunX
 * @date 2022-05-11
 */
@Configuration
public class RedisMqConfig {

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter... listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // 添加订阅频道
        container.addMessageListener(listenerAdapter[0], new PatternTopic(RedisMqChannelConstant.KEY_REDIS_MSG_CHANNEL_LOG_API));
        return container;
    }

    @Bean
    public MessageListenerAdapter logApiListenerAdapter(LogApiListener logApiListener) {
        return new MessageListenerAdapter(logApiListener);
    }

    @Bean
    public StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }

}
